<?php


namespace app\admin\model;


use app\admin\model\TeacherForum as TeacherForumModel;
use app\common\lib\RespCode;
use app\common\model\BaseModel;
use custom\Err;
use oss\Oss;

class TeacherForum extends BaseModel {

    /**
     * 列表
     * @param $params
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getList($params) {
        $count = self::count();
        if (empty($count)) {
            $result = [
                'page'    => 1,
                'pageNum' => 1,
                'count'   => 0,
                'data'    => [],
            ];
        } else {
            $page = empty($params['page']) ? 1 : intval($params['page']);
            $long = empty($params['long']) ? 10 : intval($params['long']);
            $data = self::page($page, $long)
                ->order('id', 'desc')
                ->select()
                ->toArray();
            $pageNum = ceil($count / $long);
            $result = [
                'page'    => $page,
                'pageNum' => $pageNum,
                'count'   => $count,
                'data'    => $data,
            ];
        }
        return $result;
    }

    /**
     * 编辑
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function post($params) {
        if(empty(trim($params['title']))){
            Err::add('标题不能为空', RespCode::PARAMS_ERR);
            return false;
        }
        if (!empty($params['id'])) {
            if (!$old = self::where('id', $params['id'])->select()->toArray()) {
                Err::add('数据不存在', RespCode::PARAMS_ERR);
                return false;
            }
        }
        if (mb_strlen(trim($params['title']), 'utf-8') > 30) {
            Err::add('标题长度限制30', RespCode::PARAMS_ERR);
            return false;
        }
        $data = [
            'title'    => trim($params['title']),
            'cover'    => $params['cover'],
            'url'      => $params['url'],
            'init_num' => mt_rand(50, 200),
        ];
        if (empty($params['id'])) {
            $data['itime'] = time();
            self::insert($data);
        } else {
            //删除图片
            if ($old[0]['url'] != $params['url'] && !empty($old[0]['url'])) {
                $oss = Oss::getInstance();
                $oss->delOneFile($old[0]['url']);
            }
            if ($old[0]['cover'] != $params['cover'] && !empty($old[0]['cover'])) {
                $oss = Oss::getInstance();
                $oss->delOneFile($old[0]['cover']);
            }
            self::where('id', $params['id'])->update($data);
        }
        return true;
    }

    /**
     * 删除
     * @param $params
     * @return bool
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public static function del($params) {
        if (!$old = self::where('id', $params['id'])->select()->toArray()) {
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        //删除图片
        if (self::where('id', $params['id'])->delete()) {
            $oss = Oss::getInstance();
            $oss->delOneFile($old[0]['url']);
            $oss->delOneFile($old[0]['cover']);
        }
        return true;
    }

    /**
     * 详情
     * @param $id
     * @return bool
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function info($id,&$_info){
        $info = self::where('id', $id)
            ->field('url,cover,title')
            ->select()
            ->toArray ();
        if(empty($info)){
            Err::add('数据不存在', RespCode::PARAMS_ERR);
            return false;
        }
        $_info=$info[0];
        return true;
    }
}
